package com.idol.util;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Supreme_Sir
 * @version 1.0
 * @className DataSourceUtil
 * @description 数据库配置工具
 * @date 2020/10/13 10:06
 **/
public class DataSourceUtil {
    private static DruidDataSource druidDataSource = new DruidDataSource();

    static {
        InputStream inputStream = DruidDataSource.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {

            properties.load(inputStream);
            druidDataSource.setDriverClassName(properties.getProperty("jdbc.driver"));
            druidDataSource.setUrl(properties.getProperty("jdbc.url"));
            druidDataSource.setUsername(properties.getProperty("jdbc.username"));
            druidDataSource.setPassword(properties.getProperty("jdbc.password"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return druidDataSource.getConnection();
    }

    private DataSourceUtil() {
    }
}
